package com.nvn.androidapp.adapters;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;

import com.nvn.androidapp.R;
import com.nvn.androidapp.Objects.ItemMenu;
import com.nvn.androidapp.utils.Statics;



public class MenuAdapter extends BaseAdapter{

	private Context mContext;
	private List<ItemMenu> items;
	private LayoutInflater inflater;
	private OnOkClickListener onOk;
	
	public MenuAdapter(Activity _context,List<ItemMenu> objects) {
		super();
		
		this.mContext = _context;
		this.items=objects;
		this.inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

	}
	
	@Override
	public View getView(final int position, View v, ViewGroup parent) {
		final ItemMenu item = getItem(position);
		
		if (v == null) {
			v = inflater.inflate(R.layout.item_grid_menu, null);
		}
		final Button btn = (Button)v;
		
		btn.setText(item.getTxt());
		Drawable d = mContext.getResources().getDrawable(item.getRes());
		d.setBounds(0, 0, Statics.PixelFromDp(mContext, 32),  Statics.PixelFromDp(mContext, 32));
		btn.setCompoundDrawables(null, d, null, null);
		
		btn.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				if(null != onOk) onOk.onOkClick(item.getTxt());
			}
		});
		
	    return v;
	}


	@Override
	public int getCount() {
		return items.size();
	}

	@Override
	public ItemMenu getItem(int position) {
		return items.get(position);
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}
	public void setData(List<ItemMenu> ob){
		this.items=ob;
		this.notifyDataSetChanged();
	}
	
	public void setOnOkClickListener(OnOkClickListener onOk){
		this.onOk = onOk;
	}
	
	public interface OnOkClickListener{
		public void onOkClick(String txt);
	}
}
